import groovy.json.JsonSlurper
import spock.lang.Specification

/**
 * Created at 2017/11/7 15:43
 * @author gongweixin
 */
class AsExample extends Specification {

    def "test map to object"() {
        def map = new JsonSlurper().parseText('{"id":10, "name":"测试", "subObj": {"name":"sub", "no":"no"}, "sub_name":"sub_name"}')
//        def obj = map as Obj
        def obj = new Obj(map)

        println obj

        expect:
        map instanceof Map
        map.id == 10
        map.id instanceof Integer

        obj instanceof Obj
        obj.id == 10L
        obj.id instanceof Long
        obj.name == "测试"
        obj.name instanceof String
//        obj.subObj instanceof SubObj
//        obj.subObj.id == null
//        obj.subObj.name == "sub"
        obj.subName == null
    }

    static class Obj {
        Long id
        String name
        SubObj subObj
        String subName
    }

    static class SubObj {
        Long id
        String name
    }
}
